import Vue from "vue";
import micAppConfig from "@/qiankun/config";
import Router, { RouteConfig } from "vue-router";
// import homeLayout from "@/layout/index.vue";
// import Childapp from "@/qiankun/components/childapp.vue";
import micAppRoutes from "./micApp/main";
import LoginView from "@/views/login/index.vue";
import NotFoundView from "@/views/404.vue";
import { RouteMeta } from "./routeMeta";
import { UserModule } from "@/store/modules/user";

// console.log("Layoutindex",homeLayout)

Vue.use(Router);

const routes: RouteConfig[] = [
  {
    path: "/login",
    component: LoginView,
    meta: new RouteMeta({ title: "登录", hidden: true, menuKey: "login" })
  },

  {
    path: "/404",
    component: NotFoundView,
    meta: new RouteMeta({ title: "404", hidden: true, menuKey: "404" })
  },
  // {
  //   path: "/",
  //   component: () => import("@/layout/index.vue"),
  //   // component: homeLayout,
  //   redirect: "/home",
  //   meta: new RouteMeta({
  //     title: "首页",
  //     menuKey: "root"
  //   }),
  //   children: [
  //     {
  //       path: "home",
  //       component: () => import("@/views/sysHome.vue"),
  //       meta: new RouteMeta({
  //         title: "首页",
  //         menuKey: "home",
  //         parentKeys: ["root"],
  //         icon: "dashboard",
  //         breadcrumb: false
  //       })
  //     }
  //     // {
  //     //   path: "child/*",
  //     //   component: () => import("@/qiankun/components/childapp.vue"),
  //     //   meta: {
  //     //     title: "子应用",
  //     //     defaultPath:"child/app1",
  //     //     alwaysShow: false
  //     //   }
  //     // }
  //   ]
  // },
  // ...micAppRoutes,
  {
    path: "*",
    redirect: "/404",
    meta: { hidden: true, menuKey: "*" }
  }
];

console.log("路由配置", routes);
const router = new Router({
  base: micAppConfig.basePath,
  mode: "history",
  scrollBehavior: (to, from, savedPosition) => {
    if (savedPosition) {
      return savedPosition;
    } else {
      return { x: 0, y: 0 };
    }
  },
  routes: routes
});

export function addRoutesConfig(routes: RouteConfig[]) {
  // 添加覆盖所有路由配置
  let routeConfig = router.options
  routes.forEach(item => {
    router.addRoute(item);
    routeConfig.routes?.push(item)
  });
  // console.log("router",router)
  // console.log("routeConfig",routeConfig)
}

export let allRoutes = routes.concat(micAppRoutes); //所有路由

try {
  UserModule.AddAllRoutes()
} catch (error) {
  router.replace("/login")
}

export default router;
